<template>
  <div class="carousel">
    <div>
      <el-button class="btn-add" round size="small" type="primary" @click="handleAdd()">添加轮播图</el-button>
    </div>
    <el-table
      ref="homeAdvertiseTable"
      :data="list"
      style="width: 100%;"
      v-loading="listLoading"
      height="100%"
      border
    >
      <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
      <el-table-column label="图片" align="center">
        <template slot-scope="scope">
          <img style="height: 80px" :src="$API + '/' + scope.row.imgPath" />
        </template>
      </el-table-column>
      <el-table-column label="启动/关闭" align="center">
        <template slot-scope="scope">
          <el-switch
            @change="handleUpdateStatus(scope.$index, scope.row)"
            :active-value="'1'"
            :inactive-value="'0'"
            v-model="scope.row.status"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="上传轮播图" :visible="dialogFormVisible" @close="dialogFormVisible=false">
      <el-form ref="form" :model="form" label-width="90px">
        <el-form-item label="上传图片:">
          <el-upload
            action
            class="upload-demo"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture"
            name="file"
            :http-request="action"
            :limit="1"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <!--  -->
        <el-form-item label="描述：">
          <el-input v-model="form.describes"></el-input>
        </el-form-item>
        <el-form-item label="启动关闭：">
          <el-switch :active-value="'1'" :inactive-value="'0'" v-model="form.status"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button type="primary" @click="addClick">添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  policeIndex,
  uploadFile,
  delCarousel,
  updateCarousel,
  addCarousel
} from "@/api/police";
export default {
  data() {
    return {
      listLoading: false,
      dialogFormVisible: false,
      list: [
        {
          carousel_id: "",
          imgPath: "",
          describes: "",
          product_id: ""
        }
      ],
      form: {
        status: "1",
        imgPath: ""
      },
      fileList: []
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      policeIndex().then(res => {
        this.list = res.data.map(item => {
          return {
            ...item,
            imgPath: item.imgPath
          };
        });
      });
    },
    handleAdd() {
      this.dialogFormVisible = true;
    },
    handleUpdateStatus(index, data) {
      updateCarousel(data).then(res => {
        if (res.code == 200) {
          this.$message.success(data.status == 0 ? "关闭成功" : "开启成功");
        } else {
          this.$message.error("更新失败");
        }
      });
    },
    handleDelete(index, data) {
      this.$confirm("此操作将永久删除该图片, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delCarousel({ id: data.carousel_id }).then(res => {
          if (res.code == 200) {
            this.$message.success("删除成功");
            this.init();
          } else {
            this.$message.error("删除失败");
          }
        });
      });
    },
    addClick() {
      if (!this.form.imgPath) return this.$message.error("请上传图片");
      if (!this.form.describes) return this.$message.error("请填写描述");
      this.form.product_id = Math.floor(Math.random() * 160 + 1);
      addCarousel(this.form).then(res => {
        if (res.code == 200) {
          this.$message.success("添加成功");
          this.form = {};
          this.init();
        } else {
          this.$message.error("添加失败");
        }
        this.dialogFormVisible = false;
      });
    },
    handleRemove(file, fileList) {
      if (fileList.length == 0) {
        this.form.imgPath = "";
      }
    },
    handlePreview(file) {
      console.log(file);
    },
    action(data) {
      let formData = new FormData();
      formData.append("file", data.file);
      uploadFile(formData).then(res => {
        this.form.imgPath = res.data.url;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.btn-add {
  margin: 0 10px 10px;
}
.carousel {
  padding: 20px;
  height: calc(100vh - 120px);
}
</style>